import game.tcturtle
import math
import datetime
import random

class CombatTurtle(game.tcturtle.TurtleParent ):
    is_turn = 0
    stationNum = 0
    xPosition = 0
    yPosition = 0
    other_no_shoot_start = datetime.datetime.now()
    other_no_shoot_end = datetime.datetime.now()
    def class_name():
        return "ct_Vitamin"
    def class_desc():
        return "Dodge the opponent in any direction and shoot the opponent after he shoots."
    def class_shape():
        return 0
    def setup(self):
        self.spd = 1.0
        self.turn_spd = 0.5
    def heading_by_angle(self,angle = 0):
        target = self.other_position
        if type(target) == int or type(target) == float:
            target = (int(self.x + 10000 * math.cos(math.radians(target))), int(self.y - 10000 * math.sin(math.radians(target))))

        if type(target) == tuple:#元组
            turn = self.relative_heading_towards(target)
            turn = turn + angle
            if turn > 180:
                turn = turn - 360
            if turn < -180:
                turn = turn + 360
            turn = min(max(turn, -self.max_turn_speed), self.max_turn_speed)
            turn /= self.max_turn_speed
            self.left(turn)

    def step(self):
        distance = self.distance()
        distance_keep = 200
        if not self._other.can_shoot:
            self.other_no_shoot_start = datetime.datetime.now()
            self.other_no_shoot_end = datetime.datetime.now()
        else:
            self.no_shoot_end = datetime.datetime.now()

        other_between_shoot_time = (self.other_no_shoot_end - self.other_no_shoot_start).seconds
        if self._other.cooldown > 35 or (self._other.cooldown < 25 and distance < distance_keep and other_between_shoot_time < 10) or (self._other.cooldown < 5 and other_between_shoot_time < 10):
            if self.is_turn == 0:
                self.is_turn = random.randint(1, 4)
            if self.is_turn == 1:
                self.heading_by_angle(90)
                self.forward()
            elif self.is_turn == 2:
                self.heading_by_angle(-90)
                self.forward()
            elif self.is_turn == 3:
                self.heading_by_angle(90)
                self.backward()
            else:
                self.heading_by_angle(-90)
                self.backward()
        else:
            self.is_turn = 0
            self.heading_by_angle(0)
            if distance > distance_keep:
                self.forward()
            else:
                self.backward()
        if self._other.cooldown == 30 or (self.relative_heading_towards() == 0 and other_between_shoot_time >= 10):
            if self.can_shoot and (self.line_of_sight()):
                self.shoot()

        if self.xPosition == self.x and self.yPosition == self.y:
            self.stationNum=1
            if self.stationNum ==1:
                self.is_turn = random.randint(1, 4)
                if self.is_turn == 1:
                    self.heading_by_angle(90)
                    self.forward()
                elif self.is_turn == 2:
                    self.heading_by_angle(-90)
                    self.forward()
                elif self.is_turn == 3:
                    self.heading_by_angle(90)
                    self.backward()
                else:
                    self.heading_by_angle(-90)
                    self.backward()
                self.stationNum = 0
        self.xPosition = self.x
        self.yPosition = self.y








